package com.longcai.fenzhuang.fuyou;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.longcai.entity.payment.fuyou.request.ClyjFuyouAgencyRecord;



public class Qrytrans {
	private static final String ENCODEING="UTF-8";
	public static void requestPost(String url,List<NameValuePair> params) throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClientBuilder.create().build();

		HttpPost httppost = new HttpPost(url);
		httppost.setEntity(new UrlEncodedFormEntity(params,ENCODEING));

		CloseableHttpResponse response = httpclient.execute(httppost);
		System.out.println(response.toString());

		HttpEntity entity = response.getEntity();
		String jsonStr = EntityUtils.toString(entity, "utf-8");
		readXML(jsonStr);
		System.out.println("---------------"+jsonStr);

		httppost.releaseConnection();
	}
	public static String readXML(String xmlResult) {
		try {
			Document doc = DocumentHelper.parseText(xmlResult); // 将字符串转为XML
			Element rootElt = doc.getRootElement(); // 获取根节点
			System.out.println("根节点：" + rootElt.getName());// 拿到根节点的名称
			List<Node> list = doc.selectNodes("trans");
			System.out.println(list);
			Iterator<Element> iterator = rootElt.elementIterator("trans"); // 获取根节点下的子节点tans
			
			

			// 遍历head节点
			/*	while (iter.hasNext()) {
				Element recordEle = (Element) iter.next();
				String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值
				System.out.println("title:"+ title);
				Iterator iters = recordEle.elementIterator("script");// 获取子节点head下的子节点script
			 */				// 遍历Header节点下的Response节点
			/*while (iterator.hasNext()){
				Element itemEle = (Element) iterator.next();
				itemEle
				String merdt=itemEle.elementTextTrim("merdt");// 拿到head下的子节点script下的字节点username的值
				String orderno = itemEle.elementTextTrim("orderno");
				String accntno=itemEle.el
				System.out.println(merdt);
				//System.out.println("orderno:" + orderno);
			}*/


		}
		catch (Exception e) {
			// TODO: handle exception
		} finally {
		}

		return xmlResult;
	}
	public static String getDate(int i){
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(new Date());
		if(i==1){
			String str = sf.format(calendar.getTime()); 
			return str;
		}else{
			calendar.add(Calendar.DAY_OF_YEAR, -1);
			String str = sf.format(calendar.getTime()); 
			return str;	 
		}	
	}

	public Qrytrans search(ClyjFuyouAgencyRecord cfap){
		try {
			String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"+
					"<qrytransreq>"+
					"<ver>1.1</ver>"+
					"<busicd>"+cfap.getBusicd()+"</busicd>"+    //AP01:代付  AC01：代收  
					"<orderno>"+cfap.getOrderno()+"</orderno>"+      //查询多个流水，流水中间用英文,间隔，一次最多50个
					"<startdt>"+Qrytrans.getDate(2)+"</startdt>"+  
					"<enddt>"+Qrytrans.getDate(1)+"</enddt>"+
					"</qrytransreq>";
			String macSource = "0002900F0345178|123456|"+xml;
			String mac = MD5Util.encode(macSource, "UTF-8").toUpperCase();
			String loginUrl = "https://fht-test.fuiou.com/fuMer/qry.do";
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("merid", "0002900F0345178"));
			params.add(new BasicNameValuePair("xml", xml));
			params.add(new BasicNameValuePair("mac", mac));

			requestPost(loginUrl,params);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		readXML("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><qrytransrsp><ret>000000</ret><memo>成功</memo><trans><merdt>20190424</merdt><orderno>12345678910</orderno><accntno>621226190400611531</accntno><accntnm>似曾相识</accntnm><amt>1000</amt><entseq></entseq><memo></memo><state>0</state><result>渠道资金到账已复核,交易未发送</result><reason></reason><tpst>0</tpst><rspcd></rspcd><transStatusDesc>acceptSuccess</transStatusDesc></trans><trans><merdt>20190424</merdt><orderno>12345678911</orderno><accntno>621226190400611531</accntno><accntnm>似曾相识</accntnm><amt>1000</amt><entseq></entseq><memo></memo><state>0</state><result>渠道资金到账已复核,交易未发送</result><reason></reason><tpst>0</tpst><rspcd></rspcd><transStatusDesc>acceptSuccess</transStatusDesc></trans></qrytransrsp>");
	}

}
